package xiaohuo.fancyring.config

import eos.moe.dragoncore.api.worldtexture.animation.Animation
import eos.moe.dragoncore.api.worldtexture.animation.RotateAnimation
import eos.moe.dragoncore.api.worldtexture.animation.ScaleAnimation
import eos.moe.dragoncore.api.worldtexture.animation.TranslateAnimation
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.function.releaseResourceFile
import taboolib.module.configuration.Configuration
import xiaohuo.fancyring.data.MonsterData
import xiaohuo.fancyring.data.PositionData
import xiaohuo.fancyring.data.RealBodyData
import xiaohuo.fancyring.data.RingData

object ConfigManager {
    var rpgPlugin = 1
    var showHand = false
    lateinit var inventorySlot: List<Int>
    lateinit var dragonSlot: List<String>
    var showCoolDown = 0L
    var showInterval = 0L
    lateinit var realBodySlot: String
    var realBodyForward = 0F
    var realBodyHeight = 0F
    var realBodySideways = 0F
    val scaleIncrease = hashMapOf<Int, Float>()

    val animationMap = hashMapOf<String, Animation>()
    val positionMap = hashMapOf<String, PositionData>()
    val ringMap = hashMapOf<String, RingData>()
    val monsterList = arrayListOf<MonsterData>()
    val realBodyList = arrayListOf<RealBodyData>()

    @Awake(LifeCycle.ENABLE)
    fun loadConfig() {
        animationMap.clear()
        positionMap.clear()
        ringMap.clear()
        monsterList.clear()
        realBodyList.clear()

        with(Configuration.loadFromFile(releaseResourceFile("Config.yml"))) {
            rpgPlugin = getInt("RpgPlugin")
            showHand = getBoolean("ShowHand")
            inventorySlot = getIntegerList("InventorySlot")
            dragonSlot = getStringList("DragonSlot")
            showCoolDown = getLong("ShowCoolDown")
            showInterval = getLong("ShowInterval")
            realBodySlot = getString("RealBodySlot")!!
            realBodyForward = getDouble("RealBodyForward").toFloat()
            realBodyHeight = getDouble("RealBodyHeight").toFloat()
            realBodyForward = getDouble("RealBodyForward").toFloat()
            getConfigurationSection("ScaleIncrease")?.let {
                it.getKeys(false).forEach { key ->
                    scaleIncrease[key.toInt()] = it.getDouble(key).toFloat()
                }
            }
        }
        with(Configuration.loadFromFile(releaseResourceFile("Animation.yml"))) {
            getKeys(false).forEach {
                animationMap[it] = with(getConfigurationSection(it)!!) {
                    when (getString("Type")) {
                        "Scale" -> {
                            ScaleAnimation().apply {
                                delay = getInt("Delay")
                                fromScale = getDouble("FromScale").toFloat()
                                toScale = getDouble("ToScale").toFloat()
                                duration = getInt("Duration")
                                cycleCount = getInt("CycleCount")
                                fixed = getBoolean("Fixed")
                                resetTime = getInt("ResetTime")
                            }
                        }

                        "Rotate" -> {
                            RotateAnimation().apply {
                                direction = getString("Direction")
                                delay = getInt("Delay")
                                angle = getDouble("Angle").toFloat()
                                duration = getInt("Duration")
                                cycleCount = getInt("CycleCount")
                                fixed = getBoolean("Fixed")
                                resetTime = getInt("ResetTime")
                            }
                        }

                        "Translate" -> {
                            TranslateAnimation().apply {
                                direction = getString("Direction")
                                delay = getInt("Delay")
                                distance = getDouble("Distance").toFloat()
                                duration = getInt("Duration")
                                cycleCount = getInt("CycleCount")
                                fixed = getBoolean("Fixed")
                                resetTime = getInt("ResetTime")
                            }
                        }

                        else -> {
                            return
                        }
                    }
                }
            }
        }
        with(Configuration.loadFromFile(releaseResourceFile("Position.yml"))) {
            getKeys(false).forEach {
                positionMap[it] = PositionData(getConfigurationSection(it)!!)
            }
        }
        with(Configuration.loadFromFile(releaseResourceFile("Ring.yml"))) {
            getKeys(false).forEach {
                ringMap[it] = RingData(getConfigurationSection(it)!!)
            }
        }
        with(Configuration.loadFromFile(releaseResourceFile("Monster.yml"))) {
            getKeys(false).forEach {
                monsterList += MonsterData(getConfigurationSection(it)!!)
            }
        }
        with(Configuration.loadFromFile(releaseResourceFile("RealBody.yml"))) {
            getKeys(false).forEach {
                realBodyList += RealBodyData(getConfigurationSection(it)!!)
            }
        }
    }
}